package com.clouddot.baipai.ui.util.dropListView;

/**
 * Created by wxy on 2018/4/21.
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.model.beam.getAllClass.allclassInfo;
import com.clouddot.baipai.ui.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NewApi")
/**
 * 下拉列表框控件
 * @author caizhiming
 *
 */
public class dropDownListView extends LinearLayout{

    private TextView editText;
    private ImageView imageView;
    private PopupWindow popupWindow = null;
    private List<allclassInfo> dataList =  new ArrayList<allclassInfo>();
    private View mView;

    public String selectId = Env.getAppContext().classId;
    public dropDownListView(Context context) {
        this(context,null);
        // TODO Auto-generated constructor stub
    }
    public dropDownListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
        // TODO Auto-generated constructor stub
    }
    public dropDownListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        initView();
    }

    public void initView(){
        String infServie = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater layoutInflater;
        layoutInflater =  (LayoutInflater) getContext().getSystemService(infServie);
        View view = null;
        if(Environment.getInstance().Height <= 100) {
            view  = layoutInflater.inflate(R.layout.h_l_dropdownlist_view, this,true);
        }else{
            view  = layoutInflater.inflate(R.layout.v_l_dropdownlist_view, this,true);
        }
        editText= (TextView)view.findViewById(R.id.text);
        imageView = (ImageView)view.findViewById(R.id.btn);
        this.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(popupWindow == null ){
                    if(!TextUtils.isEmpty(Env.getAppContext().host)){
                        showPopWindow();
                    }else{
                        ToastUtil.showToast(Env.getContent(),"请先设置端口号");
                    }
                }else{
                    closePopWindow();
                }
            }
        });
    }
    /**
     * 打开下拉列表弹窗
     */
    public void showPopWindow() {
        // 加载popupWindow的布局文件
        String infServie = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater layoutInflater;
        layoutInflater =  (LayoutInflater) getContext().getSystemService(infServie);
        View contentView = null;
        if(Environment.getInstance().Height <= 100){
             contentView  = layoutInflater.inflate(R.layout.h_l_dropdownlist_popupwindow, null,false);
        }else{
            contentView  = layoutInflater.inflate(R.layout.v_l_dropdownlist_popupwindow, null,false);

        }
        ListView listView = (ListView)contentView.findViewById(R.id.listView);

        listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));
        popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_bg));
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(this);
    }
    /**
     * 关闭下拉列表弹窗
     */
    public void closePopWindow(){
        if(popupWindow != null){
            popupWindow.dismiss();
            popupWindow = null;
        }
    }
    /**
     * 设置数据
     * @param list
     */
    public void setItemsData(List<allclassInfo> list){
        dataList = list;
        if(!TextUtils.isEmpty(Env.getAppContext().className)){
            editText.setText(Env.getAppContext().className);
        }else{
            editText.setText("点击选择班级进行绑定");
        }
    }
    /**
     * 数据适配器
     * @author caizhiming
     *
     */
    class XCDropDownListAdapter extends BaseAdapter{

        Context mContext;
        List<allclassInfo> mData;
        LayoutInflater inflater;
        public XCDropDownListAdapter(Context ctx,List<allclassInfo> data){
            mContext  = ctx;
            mData = data;
            inflater = LayoutInflater.from(mContext);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // 自定义视图
            ListItemView listItemView = null;
            if (convertView == null) {
                // 获取list_item布局文件的视图
                if(Environment.getInstance().Height <= 100){
                    convertView = inflater.inflate(R.layout.h_l_dropdown_list_item, null);

                }else{
                    convertView = inflater.inflate(R.layout.v_l_dropdown_list_item, null);

                }
                listItemView = new ListItemView();
                // 获取控件对象
                listItemView.tv = (TextView) convertView
                        .findViewById(R.id.tv);

                listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
                // 设置控件集到convertView
                convertView.setTag(listItemView);
            } else {
                listItemView = (ListItemView) convertView.getTag();
            }

            // 设置数据
            listItemView.tv.setText(mData.get(position).getGradeClassName().toString());
            final String text = mData.get(position).getGradeClassName().toString();
            listItemView.layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    editText.setText(text);
                    selectId = mData.get(position).getClassId();
                    closePopWindow();
                }
            });
            return convertView;
        }

    }
    private static class ListItemView{
        TextView tv;
        LinearLayout layout;
    }

}
